/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.common.utils;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 *@Description Md5 加密工具
 *@Author 操文健
 *@Date 2018-09-06 下午3:57
 *@Version 1.0
 **/
public class MD5Utils {
    // 加密字符串
    private static final String SALT = "!@#¥%&*";

    private static final String ALGORITH_NAME = "md5";

    // hash加密的次数
    private static final int HASH_ITERATIONS = 2;

    /**
     * 对密码加密
     * @param pswd
     * @return
     */
    public static String encrypt(String pswd) {
        String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(SALT), HASH_ITERATIONS).toHex();
        return newPassword;
    }

    /**
     * 根据用户名作为加密字符串对密码进行加密
     * @param username
     * @param pswd
     * @return
     */
    public static String encrypt(String username, String pswd) {
        String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username + SALT),
                HASH_ITERATIONS).toHex();
        return newPassword;
    }

    // 测试加密的密码
    /*public static void main(String[] args) {

        System.out.println(MD5Utils.encrypt("admin", "admin123"));
        System.out.println(MD5Utils.encrypt("admin123"));
    }*/
}
